"""
Case Type   : cm_uninstall
Case Name   : cm_uninstall执行失败后，再次执行cm_uninstall
Create At   : 2025/4/30
Owner       : ningyali
Description :
    1.取消配置xml文件中的cm相关参数
    2.数据库用户在主节点执行cm_uninstall，指定错误的x参数
    3.cm_ctl查看集群状态
    4.gs_om查看集群状态
    5.查看数据库用户下进程和定时任务
    6.主备节点下查看CMlog目录
    7.主备节点下查看CM数据目录
    8.主备节点下查看CM相关二进制文件
    9.再次执行cm_uninstall
    10.cm_ctl查看集群状态
    11.gs_om查看集群状态
Expect      :
    1.执行成功
    2.卸载失败
    3.cm未卸载
    4.cm未卸载
    5.有cm_server,om_monitor,cm_agent,定时任务
    6.未删除
    7.未删除
    8.未删除
    9.卸载成功
    10.失败,未安装cm
    11.未安装cm
History     :
"""
import os
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node_names = (
            'PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
        self.sh = [CommonSH(n) for n in self.user_node_names]
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = [Node(n) for n in self.user_node_names]
        self.common = Common()
        self.constant = Constant()
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.sh[0].node.ssh_user)
        self.un_cm = CmUnInstall(self.install_pkg_path,
                                 self.sh[0].node.ssh_user)

        text = '----prestep1:安装cm;expect:执行成功----'
        self.log.info(text)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')

        res = self.cm.cm_install(self.user_nodes[0])
        self.assertIn('Install CM tool success', res, f'执行失败: {text}')

    def test_cm_uninstall(self):
        text = '----step1:取消配置xml文件中的cm相关参数 expect:执行成功----'
        self.log.info(text)
        sed_cmd = f'sed -i \'/cm/d\' {self.cm.xml_path};'
        sed_res = self.common.get_sh_result(self.user_nodes[0], sed_cmd)
        self.assertNotIn('error', sed_res.lower(), "执行失败:" + text)

        text = '----step2:数据库用户在主节点执行cm_uninstall，指定错误的x参数 expect:卸载失败----'
        self.log.info(text)
        res = self.un_cm.cm_uninstall(
            self.user_nodes[0],
            f'-x -e {macro.DB_ENV_PATH} --deleteBinary --deleteData')
        expect = '[GAUSS-50000] : Unrecognized parameter: ' \
                 'option -x not recognize'
        self.assertIn(expect, res, f'执行失败: {text}')

        text = '----step3:cm_ctl查看集群状态;expect:cm未卸载----'
        self.log.info(text)
        query_cmd = f'source {macro.DB_ENV_PATH}; cm_ctl query -Civdp'
        status_msg = self.common.get_sh_result(self.user_nodes[0], query_cmd)
        self.assertIn("CMServer State", status_msg, "执行失败:" + text)

        text = '----step4:gs_om查看集群状态;expect:cm未卸载----'
        self.log.info(text)
        status_msg = self.sh[0].get_db_cluster_status('detail')
        self.log.info(status_msg)
        self.assertIn("CMServer State", status_msg, "执行失败:" + text)

        text = '----step5:查看数据库用户下进程和定时任务;' \
               'expect:有cm_server,om_monitor,cm_agent,定时任务----'
        self.log.info(text)
        show_cmd = "crontab -l;ps ux|grep -v grep|" \
                   "egrep 'cm_server|cm_agent|om_monitor';"
        process_res = self.sh[0].exec_gs_ssh(show_cmd)
        self.assertIn('bin/cm_server', process_res, "执行失败:" + text)
        self.assertIn('bin/cm_agent', process_res, "执行失败:" + text)
        self.assertIn('om_monitor -L', process_res, "执行失败:" + text)

        text = '----step6:主备节点下查看CMlog目录;expect:未删除----'
        self.log.info(text)
        ls_cmd = f"source {macro.DB_ENV_PATH};ls $GAUSSLOG/cm;"
        ls_res = self.sh[0].exec_gs_ssh(ls_cmd)
        self.assertNotIn('No such file or directory', ls_res, "执行失败:" + text)

        text = '----step7:主备节点下查看CM数据目录;expect:未删除----'
        self.log.info(text)
        ls_cmd = f"source {macro.DB_ENV_PATH};ls {self.cm.cm_path};"
        ls_res = self.sh[0].exec_gs_ssh(ls_cmd)
        self.assertNotIn('No such file or directory', ls_res, "执行失败:" + text)

        text = '----step8:主备节点下查看CM相关二进制文件;expect:未删除----'
        self.log.info(text)
        ls_cmd = f"source {macro.DB_ENV_PATH};" \
                 f"ls $GAUSSHOME/bin/cm_ctl;" \
                 f"ls $GAUSSHOME/bin/cm_server;" \
                 f"ls $GAUSSHOME/bin/cm_agent;" \
                 f"ls $GAUSSHOME/bin/om_monitor"
        ls_res = self.sh[0].exec_gs_ssh(ls_cmd)
        self.assertNotIn('No such file or directory', ls_res, "执行失败:" + text)

        text = '----step9:再次执行cm_uninstall expect:卸载成功----'
        self.log.info(text)
        res = self.un_cm.cm_uninstall(
            self.user_nodes[0],
            f'-e {macro.DB_ENV_PATH} --deleteBinary --deleteData')
        self.assertIn('Uninstall CM tool success', res, f'执行失败: {text}')

        text = '----step10:cm_ctl查看集群状态;expect:失败,未安装cm----'
        self.log.info(text)
        status_msg = self.common.get_sh_result(self.user_nodes[0], query_cmd)
        self.assertIn("cm_ctl: command not found", status_msg,
                      "执行失败:" + text)

        text = '----step11:gs_om查看集群状态;expect:未安装cm----'
        self.log.info(text)
        status_msg = self.sh[0].get_db_cluster_status('detail')
        self.log.info(status_msg)
        self.assertNotIn("CMServer State", status_msg, "执行失败:" + text)

    def tearDown(self):
        status_msg = self.sh[0].get_db_cluster_status('detail')
        self.log.info(status_msg)
        if 'CMServer State' in status_msg:
            text = '----poststep1:卸载cm;expect:执行成功----'
            self.log.info(text)
            self.un_cm = CmUnInstall(self.install_pkg_path,
                                     self.sh[0].node.ssh_user)
            res = self.un_cm.cm_uninstall(
                self.user_nodes[0], '--deleteData --deleteBinary')

            status_msg = self.sh[0].get_db_cluster_status('detail')
            self.log.info(status_msg)

            self.assertIn('Uninstall CM tool success', res, f'执行失败: {text}')
        self.assertNotIn("CMServer State", status_msg)
        self.log.info(f'----{os.path.basename(__file__)} end----')
